<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *知识
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'项目列表'),
	array('op'=>'edit','title'=>'添加项目'),
);
$return['project_status']=$this->project_status;
$return['check_status']=$this->check_status;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$this->user_id);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['cname'])){
		$params['cname like']='%'.trim($_RQ['cname']).'%';
		$return['params']['cname']=$_RQ['cname'];
	}
	if(!empty($_RQ['sn'])){
		$params['sn like']='%'.trim($_RQ['sn']).'%';
		$return['params']['sn']=$_RQ['sn'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kexie_project',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_kexie_project_cuser',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['user_id']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['member']=$member;
			$item['user']=$user;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'这里只显示待专家审核的项目，和刚审核的项目！','edit'=>true,'list'=>array(
			array('name'=>'sn','title'=>'项目编码','type'=>'string'),
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'user.cname','title'=>'公司名称','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'project_status'),
			array('name'=>'fa_starttime','title'=>'发售起始时间','type'=>'time'),
			array('name'=>'fa_edntime','title'=>'发售结束时间','type'=>'time'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
		),'action' =>array(
			 //array('type'=>'post','op'=>'daobaobiao','title'=>'导出申报表','show'=>array('status'=>array(1,8))),
			 array('type'=>'href','op'=>'shbaobiao','title'=>'上传申报表','show'=>array('status'=>array(1,8))),
			 array('type'=>'href','op'=>'tianxieyi','title'=>'填报协议书','show'=>array('status'=>3)),
			 //array('type'=>'post','op'=>'daoxieyi','title'=>'导出协议书','show'=>array('status'=>array(5,9))),
			 array('type'=>'href','op'=>'shxieyi','title'=>'上传协议书','show'=>array('status'=>array(5,9))),
		)),
		'search'=>array(
			array('name'=>'sn','width'=>15,'title'=>'项目编码','type'=>'string'),
			array('name'=>'cname','width'=>15,'title'=>'公司名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_project',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3);
	}
	$cuser=pdo_get('jy_kexie_project_cuser',array('id'=>$this->user_id,'uniacid'=>$_SESSION['uniacid']));
	if(!$cuser['cname'] || !$cuser['mobile'] || !$cuser['cimage'] || !$cuser['username']){
		exi('请先完善个人公司信息（联系人，联系电话，公司名称，营业执照等）','error','/gongsi.index/');
	}
	$item['zm_image']=$item['zm_image']?json_decode($item['zm_image'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			//array('name'=>'cname','title'=>'公司名称','type'=>'string'),
			//array('name'=>'username','title'=>'联系人','type'=>'string'),
			//array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			//array('name'=>'code','title'=>'身份账号','type'=>'string'),
			//array('name'=>'cimage','title'=>'营业执照','type'=>'image'),
			array('name'=>'anli','title'=>'以往案例','type'=>'richtext'),
			array('name'=>'zm_image','title'=>'证明文件','type'=>'images'),
			array('name'=>'wname','title'=>'委托人','type'=>'string'),
			array('name'=>'pid','title'=>'申报大项','type'=>'select2','options'=>'groups','title2'=>'子项','name2'=>'cid','options2'=>'cids'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'project_status'),
		)
	);
	$return['groups']=pdo_getall('jy_kexie_project_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>0),array('id','title'),'id');
	foreach($return['groups'] as &$gv){
		$gv['cids']=pdo_getall('jy_kexie_project_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$gv['id']),array('id','title'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['status'] && $postdata['status']!=10){
		exi('项目已进入审核流程，不能编辑！','error');
	}
	if(!$postdata['title']){
		exi('请填写项目名称！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'anli'=>trim($postdata['anli']),
		'zm_image'=>ijson_encode($postdata['zm_image']),
		'wname'=>trim($postdata['wname']),
		'type'=>intval($postdata['type']),
		'pid'=>intval($postdata['pid']),
		'cid'=>intval($postdata['cid']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_kexie_project',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$sn='SN'.get_ordersn($this->tablename('project'),'sn');
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['sn']=$sn;
		$data['user_id']=$this->user_id;
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_kexie_project',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='daobaobiao'){
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$url=download_url('jy_kexie/gongsi.project/down_baobiao/id='.$_RQ['id']);
	exi('','',$url);
}
if($_RQ['op']=='down_baobiao'){
	$this->model('project')->export_detail($_RQ['id']);
	exi($return);
}
if($_RQ['op']=='shbaobiao'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['baobiaofile']){
			exi('请上传报表！','error');
		}
		pdo_update('jy_kexie_project',array('baobiaofile'=>$post['baobiaofile'],'status'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('处理成功！','','list');
	}
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$item['baobiaodemo']=SITEROOT.'/addons/jy_kexie/resource/baobiaodemo.docx';
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'上传报表',
		'edit'=>array(
			array('name'=>'baobiaodemo','title'=>'申报表模板','type'=>'copy'),
			array('name'=>'baobiaofile','title'=>'上传报表','type'=>'upfile'),
		),
	);
	exi($return);
}
if($_RQ['op']=='tianxieyi'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['xieyifile']){
			exi('请填协议！','error');
		}
		pdo_update('jy_kexie_project',array('xieyifile'=>$post['xieyifile'],'status'=>4),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('处理成功！','','list');
	}
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'填协议',
		'edit'=>array(
			array('name'=>'xieyifile','title'=>'协议文件','type'=>'upfile'),
		),
	);
	exi($return);
}
if($_RQ['op']=='daoxieyi'){
	exi('开发中！','error');
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	exi($return);
}
if($_RQ['op']=='shxieyi'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['xieyifile']){
			exi('请上传协议！','error');
		}
		pdo_update('jy_kexie_project',array('xieyifile'=>$post['xieyifile'],'status'=>6),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('处理成功！','','list');
	}
	$item=$this->get('project',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定项目不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'上传协议',
		'edit'=>array(
			array('name'=>'xieyifile','title'=>'上传协议','type'=>'upfile'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	exi('不能删除项目！','error');
}